package com.team.employment.web.app.controller.policie;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.team.employment.common.result.Result;
import com.team.employment.model.entity.PolicieInfo;

import com.team.employment.web.admin.custom.check.IdIsEmptyCheck;
import com.team.employment.web.admin.service.PolicieInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "政策信息管理")
@RestController
@RequestMapping("/app/policyInfo")
public class policyInfoController {

    @Autowired
    private PolicieInfoService policieInfoService;

    @Autowired
    private IdIsEmptyCheck idIsEmptyCheck;

    @Operation(summary = "增加或修改政策信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody PolicieInfo policieInfo){
        policieInfoService.saveOrUpdate(policieInfo);
        return Result.ok();
    }

    @Operation(summary = "删除政策信息")
    @DeleteMapping("removeById")
    public Result removeById(@PathVariable Long id){
        idIsEmptyCheck.idIsEmpty(policieInfoService,id);
        policieInfoService.removeById(id);
        return Result.ok();
    }

    @Operation(summary = "根据policie_id得到政策信息")
    @GetMapping("getInfoById/{id}")
    public Result<PolicieInfo> getInfoById(@PathVariable Long id){
        idIsEmptyCheck.idIsEmpty(policieInfoService,id);
        PolicieInfo policieInfo = policieInfoService.getById(id);
        return Result.ok(policieInfo);
    }

    @Operation(summary = "分页查询所有政策信息")
    @GetMapping("pagePolicyInfo")
    public Result<IPage<PolicieInfo>> pagePolicyInfo(@RequestParam long current, @RequestParam long size ){
        Page<PolicieInfo> page = new Page<>(current, size);
        IPage<PolicieInfo> infoPage = policieInfoService.page(page);
        return Result.ok(infoPage);
    }

    @Operation(summary = "得到最新五个政策信息")
    @GetMapping("pagePolicyInfoNew")
    public Result<List<PolicieInfo>> pagePolicyInfoNew(){
        LambdaQueryWrapper<PolicieInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(PolicieInfo::getCreateTime).last("limit 5");
        List<PolicieInfo> list = policieInfoService.list(queryWrapper);
        return Result.ok(list);
    }
}
